// components/user/avatar.js
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    avatarUrl: "",
    size : {
      type: String|Number,
      value : 100
    }
  },
  observers: {
    'avatarUrl': function (val) {
    var  fullavatarUrl =""
      if(val){
       // fullavatarUrl  
       fullavatarUrl= val
      } 
   
      this.setData({
        fullavatarUrl
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    fullavatarUrl:"",
    isuping: -1,   //-1没状态    1上传中，  -2上传失败   2上传成功
    temurl:'http://fooddns.zuofushi.com/images/2/2018/07/B296LaGKz6JpSDAhoGD2KPHOK29jeK.png',
    t:0,
    errormessage:"",

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // handleAgreePrivacyAuthorization() {
    //   var that=this;
    //   if(wx.onNeedPrivacyAuthorization){
    //     wx.onNeedPrivacyAuthorization((resolve, eventInfo) => {
    //       console.log('触发本次事件的接口是：' + eventInfo.referrer)
    //       // 需要用户同意隐私授权时
    //       // 弹出开发者自定义的隐私授权弹窗
    //       that.selectComponent("#privacypopup").show()
         
    //       this.resolvePrivacyAuthorization = resolve
    //     })
    //   }

    //   // 用户同意隐私协议事件回调
    //   // 用户点击了同意，之后所有已声明过的隐私接口和组件都可以调用了
    //   // wx.getUserProfile()
    //   // wx.chooseMedia()
    //   // wx.getClipboardData()
    //   // wx.startRecord()
    // },
    // agree() {
    //   // 用户点击同意按钮后
    //   this.resolvePrivacyAuthorization({ buttonId: 'agree-btn', event: 'agree' })
    //   // 用户点击同意后，开发者调用 resolve({ buttonId: 'agree-btn', event: 'agree' })  告知平台用户已经同意，参数传同意按钮的id
    //   // 用户点击拒绝后，开发者调用 resolve({ event:'disagree' }) 告知平台用户已经拒绝
    // },
    // disagree(){
    
    //   this.resolvePrivacyAuthorization({event:'disagree' })

    //    wx.showToast({
    //      type:"error",
    //      title: '已拒绝',
    //    })
    // },
    bindchooseavatar(ev) {
     console.log("ev")
     console.log(ev)
      this.handleAgreePrivacyAuthorization()
      var that = this;
      var avatarUrl = ev.detail.avatarUrl
      this.setData({isuping:1,temurl: ev.detail.avatarUrl, realavatarUrl: "" })
      var app = getApp();
       var t = setTimeout(function () {

        if(that.data.t ==t){
          that.setData({isuping:-2})
        }
         
       },10000)

       this.data.t =t

 

      app.util.uploadFile(avatarUrl).then(res => {

    

        that.setData({errormessage:JSON.stringify(res)})

       
        this.data.t =0
        if(res["status"]==1){
          that.setData({isuping:2})
          that.triggerEvent('change',res["obj"]) 
        }
        else
        {
          that.setData({isuping:-2})
        }
      
      })

    },
  }
})
